/**
 * 
 */
package com.peking.donations.base.util;


import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

/**
 *
 */
public abstract class FileDownloadUtils {
	/**
	 * 根据不同浏览器将文件名中的汉字转为UTF8编码的串,以便下载时能正确显示另存的文件名.
	 *
	 * @param fileName
	 *            原文件名
	 * @return 重新编码后的文件名
	 */
	public static String fileNameToUtf8String(HttpServletRequest request, String fileName) {
		String agent = request.getHeader("User-Agent");
		try {
			boolean isFireFox = (agent != null && agent.toLowerCase().indexOf("firefox") != -1);
			if (isFireFox) {
				fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
			} else {
				fileName = StringUtils.toUtf8String(fileName);
				if ((agent != null && agent.indexOf("MSIE") != -1)) {
					// see http://support.microsoft.com/default.aspx?kbid=816868
					if (fileName.length() > 150) {
						// 根据request的locale 得出可能的编码
						fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
					}
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return fileName;
	}

}
